# .github/workflows/issues2md.yml
# https://github.com/mattduck/gh2md/issues/11

# note: the secret "GITHUB_TOKEN"
# is passed automatically.
# you only have to make sure
# to give write permissions
# to the action:
# repo > settings > actions >
# Workflow permissions >
# Read and write permissions >
# Save
# see also
# https://github.com/ad-m/github-push-action/issues/96#issuecomment-889984928

name: Issues2Markdown
on:
  # allow to run the workflow manually
  workflow_dispatch:
  #push: # comment it to reduce update.
  schedule:
    # every day
    - cron: "0 0 * * *"
    # every hour
    #- cron: "0 * * * *"
jobs:
  build:
    name: Backup github issues to markdown files
    runs-on: ubuntu-latest
    steps:
    - name: Set output path
      run: echo "GH2MD_OUTPUT_PATH=issues/github/" >> $GITHUB_ENV
    - name: Check output path
      run: |
        if ! [[ "$GH2MD_OUTPUT_PATH" =~ ^[a-zA-Z0-9_/.+~-]+$ ]]; then
          echo "error: output path does not match the pattern ^[a-zA-Z0-9_/.+~-]+$"
          exit 1
        fi
    - name: checkout
      uses: actions/checkout@master
      with:
        persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token
        fetch-depth: 0 # otherwise, you will failed to push refs to dest repo
    - name: Run gh2md
      run: |
        pip3 install --user --upgrade setuptools
        pip3 install --user gh2md
        export PATH="$HOME/.local/bin:$PATH"
        gh2md --version || true
        export GITHUB_ACCESS_TOKEN=${{ secrets.GITHUB_TOKEN }}
        # fix: RuntimeError: Output directory already exists and has files in it
        git rm -rf $GH2MD_OUTPUT_PATH || true
        # workaround for https://github.com/mattduck/gh2md/pull/31
        mkdir -p $GH2MD_OUTPUT_PATH || true
        gh2md $GITHUB_REPOSITORY $GH2MD_OUTPUT_PATH/ghmd --idempotent --multiple-files --file-extension .ghmd
        #sudo apt-get install pandoc # pandoc 2.5 == too old
    - name: install pandoc
      uses: nikeee/setup-pandoc@v1
    - name: "pandoc: convert github-markdown to strict-markdown"
      run: |
        pandoc --version || true
        mkdir -p $GH2MD_OUTPUT_PATH/md
        find $GH2MD_OUTPUT_PATH/ghmd -name '*.ghmd' -type f | while read ghmd_path
        do
          echo "ghmd_path: $ghmd_path"
          base=$(basename $ghmd_path)
          base=${base%.*}
          md_path=$GH2MD_OUTPUT_PATH/md/$base.md
          #pandoc --verbose -f gfm+hard_line_breaks -t markdown_strict --wrap preserve $ghmd_path -o $md_path
          pandoc --verbose -f gfm+hard_line_breaks -t markdown_strict $ghmd_path -o $md_path
        done
    #- name: "cleanup: move .ghmd files to separate folder"
    #  run: |
    #    mkdir -p $GH2MD_OUTPUT_PATH/ghmd/
    #    mv -v $GH2MD_OUTPUT_PATH*.ghmd $GH2MD_OUTPUT_PATH/ghmd/
    # remove .ghmd files to save space
    # usually, the .ghmd files can be reconstructed from the .md files
    - name: "cleanup: remove .ghmd files"
      run: |
        rm -rf $GH2MD_OUTPUT_PATH/ghmd
    - name: Build index readme.md
      run: |
        ./.github/workflows/issues2md-build-index.sh $GH2MD_OUTPUT_PATH/md $GH2MD_OUTPUT_PATH/readme.md
    - name: Commit files
      run: |
        git add $GH2MD_OUTPUT_PATH
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        if ! git commit -m "up $GH2MD_OUTPUT_PATH" -a
        then
          echo nothing to commit
          exit 0
        fi
    - name: Get branch name
      shell: bash
      run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
      id: get_branch
    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: ${{ steps.get_branch.outputs.branch }}
